﻿using AWSV2.ViewModels;
using Common.Model;
using Common.Utility.AJ.MobileConfiguration;
using MaterialDesignExtensions.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using static Common.AJControls.AJMultiSelect;

namespace AWSV2.Views
{
    /// <summary>
    /// WeighFormView.xaml 的交互逻辑
    /// </summary>
    public partial class WeighFormView : UserControl
    {
        public WeighFormView()
        {
            InitializeComponent();
        }

        private void Autocomplete_SearchTermInputCompleted(object sender, MaterialDesignExtensions.Controls.SearchTermInputCompletedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(e.Search))
            {
                return;
            }
            if (sender is Autocomplete ctrl)
            {
                ctrl.Dispatcher.Invoke(() =>
                {
                    var source = ctrl.AutocompleteSource as Common.Models.AJMultiSelectOptionItemAutocompleteSource;
                    var item = source.AddIfNotExist(e.Search);
                    if (ctrl.SelectedItem == null)
                    {
                        ctrl.SelectedItem = item;
                    }
                });
            }

        }

        private void Autocomplete_SelectedItemChanged(object sender, MaterialDesignExtensions.Controls.SelectedItemChangedEventArgs e)
        {
            if (sender is Autocomplete ctrl)
            {
                if (ctrl.DataContext is DynamicPoundFieldItem item)
                {
                    var @param = string.Empty;
                    if (e.SelectedItem is AJMultiSelectOptionItem option)
                    {
                        @param = option.Value.ToString();
                    }

                    if (item.Field == nameof(Common.Models.WeighingRecord.Ch))
                    {
                        (this.DataContext as WeighFormViewModel).RaiseManualInputCarNoEvent(@param);
                    }

                    if (item.Field == nameof(Common.Models.WeighingRecord.Wz))
                    {
                        (this.DataContext as WeighFormViewModel).GoodsChangeAsync(@param);
                    }

                    if (item.Field == nameof(Common.Models.WeighingRecord.Kh))
                    {
                        (this.DataContext as WeighFormViewModel).CustomerChangeAsync(@param);
                    }

                    (this.DataContext as WeighFormViewModel).TriggerRelationCheckAsync(item, @param);
                }
            }
        }

        private void Autocomplete_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (sender is Autocomplete ctrl)
            {
                if (ctrl.DataContext is DynamicPoundFieldItem item
                    && item.Field == nameof(Common.Models.WeighingRecord.Ch))
                {
                    (this.DataContext as WeighFormViewModel).CarPlateNumberKeyboardVisible = true;
                }
            }
        }

        private void TextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter) 
            {
                if (sender is TextBox tb && tb.DataContext is DynamicPoundFieldItem item)
                {
                    (this.DataContext as WeighFormViewModel).TriggerRelationCheckAsync(item, tb.Text);
                }
            }
            e.Handled = true;
        }
    }
}
